public class LC_1047 {
    public String removeDuplicates(String s) {
        StringBuffer stack = new StringBuffer();
        int i = 0;
        int top = -1;
        while(i < s.length()){
            char ch = s.charAt(i);
            if(top < 0 || stack.charAt(top) != ch){
                stack.append(ch);
                ++top;
            }else{
                stack.deleteCharAt(top);
                --top;
            }
            i++;
        }
        return stack.toString();
    }
}
